package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.PropertiesUtil;
import com.mongodb.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class OneceCreateOrUpdateRticMapJc {
	private static JSONObject prop = PropertiesUtil.getProperties();
	private static MongoClient mongoClient = null;
	private static DB db = null;
	private static DBCollection rtic_map_jc_Col = null;

//	static {
//		// 获取 mongo 的连接
//		ServerAddress serverAddress = new ServerAddress(prop.getString("mongoHost"), prop.getInteger("mongoPort"));
//		mongoClient = new MongoClient(serverAddress);
//		db = mongoClient.getDB(prop.getString("mongoDb"));
//		rtic_map_jc_Col = db.getCollection("rtic_jc");
//		rtic_map_jc_Col.drop();
//	}

	public static void doRun(JSONObject json) {
		try {
			List<DBObject> objList = new ArrayList<>();
			int count = 0;
			Iterator<String> it = json.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				JSONObject temp = json.getJSONObject(key);
				temp.put("key", key);
				DBObject obj = new BasicDBObject();
				Iterator<String> names = temp.keySet().iterator();
				while (names.hasNext()) {
					String name = names.next();
					obj.put(name, temp.get(name));
				}
				objList.add(obj);
				count++;
				if (count % 2000 == 0) {
					System.out.println("convert mongo count:" + count);
					// 存mongo
					rtic_map_jc_Col.insert(objList);
					objList.clear();
				}
			}
			if (objList.size() > 0) {
				rtic_map_jc_Col.insert(objList);
				objList.clear();
			}
			rtic_map_jc_Col.createIndex("mesh_code");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
